﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SuperFrame.Charts
{
    public class Legend : ItemsControl
    {

        public Legend()
        {
            this.DefaultStyleKey = typeof(Legend);
            this.Loaded += Legend_Loaded;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ContentPresenter conter = (ContentPresenter)this.GetTemplateChild("Part_LegendTitleTmeplate");
            if (conter != null)
                conter.SetResourceReference(ContentPresenter.ContentTemplateProperty, "LegendTitleTmeplate");
            if (Title == null)
            {
                conter.Visibility = Visibility.Collapsed;
            }
        }

        void Legend_Loaded(object sender, RoutedEventArgs e)
        {
            ChangeOrientation();
        }

        public Chart Chart
        {
            get { return (Chart)GetValue(ChartProperty); }
            set { SetValue(ChartProperty, value); }
        }
        public static readonly DependencyProperty ChartProperty = DependencyProperty.Register("Chart", typeof(Chart), typeof(Legend), new PropertyMetadata(null));


        internal Orientation LegendOrientaion
        {
            get { return (Orientation)GetValue(LegendOrientaionProperty); }
            set { SetValue(LegendOrientaionProperty, value); }
        }
        internal static readonly DependencyProperty LegendOrientaionProperty = DependencyProperty.Register("LegendOrientaion", typeof(Orientation), typeof(Legend), new PropertyMetadata(Orientation.Horizontal, OnLegendOrientationChanged));
        private static void OnLegendOrientationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            (sender as Legend).ChangeOrientation();
        }
        internal void ChangeOrientation()
        {
            ItemsPresenter itemsPresenter = GetVisualChild<ItemsPresenter>(this);
            if (itemsPresenter != null)
            {
                if (VisualTreeHelper.GetChildrenCount(itemsPresenter) > 0)
                {
                    StackPanel itemsPanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as StackPanel;
                    if (itemsPanel != null)
                    {
                        itemsPanel.Orientation = this.LegendOrientaion;
                    }
                }
            }
        }

        private static T GetVisualChild<T>(DependencyObject parent) where T : DependencyObject
        {
            T child = default(T);

            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

        public Dock Dock
        {
            get { return (Dock)GetValue(DockProperty); }
            set { SetValue(DockProperty, value); }
        }
        public static readonly DependencyProperty DockProperty = DependencyProperty.Register("Dock", typeof(Dock), typeof(Legend), new PropertyMetadata(Dock.Top, OnDockChanged));
        private static void OnDockChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            (sender as Legend).DockChanged(args);
        }
        internal void DockChanged(DependencyPropertyChangedEventArgs args)
        {
            DockPanel.SetDock(this, this.Dock);

            switch (this.Dock)
            {
                case Dock.Bottom:
                case Dock.Top:
                    this.LegendOrientaion = Orientation.Horizontal;
                    break;
                case Dock.Left:
                case Dock.Right:
                    this.LegendOrientaion = Orientation.Vertical;
                    break;
                default:
                    break;
            }

        }

        public object Title
        {
            get { return (object)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(Legend), new PropertyMetadata(null));


        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(Legend), new PropertyMetadata(new CornerRadius(0)));


        public bool ShowIcon
        {
            get { return (bool)GetValue(ShowIconProperty); }
            set { SetValue(ShowIconProperty, value); }
        }
        public static readonly DependencyProperty ShowIconProperty = DependencyProperty.Register("ShowIcon", typeof(bool), typeof(Legend), new PropertyMetadata(true));

    }
}
